import Platform from "./platform.js"
import ModelConnection from "./modelConnection.js"
import { WORDS } from './WORDS.js'
import {Plane} from './plane.js'

export default class Frame {
    platforms: Map<string, Platform>
    // 已有的平台Y坐标-200
    offY = -200
    modelConnections: ModelConnection[] = []
    curPlatformIndex = 0
    constructor(offY?: number) {
        offY && (this.offY = offY)
        this.platforms = new Map<string, Platform>()
    }

    addPlatform(plane: Plane) {
        // 已有的平台顺序+1
        this.platforms.forEach(i => {
            i.setIndex(i.index + 1)
        })

        const p = new Platform(0, plane)// b-2
        if (this.platforms.has(p.name)) {
            throw new Error(WORDS.ERROR.NONE_PLATFORM);
        }
        this.platforms.set(p.name, p)
        this.curPlatformIndex = p._index
        return p
    }


    getCurPlatform() {
        return this.getPlatform(this.curPlatformIndex)
    }

    getPlatform(index: number) {
        return Array.from(this.platforms.values()).find(i => {
            return i.index === index
        })
    }



    toJSON() {
        const frameJSON: any = {
            offY: this.offY,
            platforms: []
        }
        this.platforms.forEach(p => {
            const ds: any[] = []
            p.devices.forEach(i => {
                ds.push(i.toJSON())
            })
            const lines: any[] = []
            p.dataLines.forEach(i => {
                lines.push(i.toJSON())
            })
            frameJSON.platforms.push({
                index: p.index,
                plane: p.plane.toJSON(),
                devices: ds,
                dataLines: lines
            })
        });
        return frameJSON
    }
}
